﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Runda.CMS.Common.Entity;
using Runda.CMS.Bll;

namespace Runda.CMS.Portal.ContractManage
{
    public partial class ItemGuarantyList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) { 
                if(Request.QueryString["ItemID"] != null && Request.QueryString["ItemID"] != ""){
                    LoadData();
                    lblItemID.Text = Request.QueryString["ItemID"];
                }
            }
        }
        protected void RepeaterItemGuaranty_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "refersh")
            {
                LoadData();
            }
        }

        private void LoadData() { 
            if (Request.QueryString["ItemID"] != null && Request.QueryString["ItemID"] != "")
            {
                RepeaterItemGuaranty.DataSource = new CtrtItemGuarantyBll().GetAll<CtrtItemGuarantyEntity>(" and ItemID ='" + Request.QueryString["ItemID"] + "'");
                RepeaterItemGuaranty.DataBind();
            }
        }

        protected void RepeaterItemGuaranty_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                Label lblItemID = e.Item.FindControl("lblItemID") as Label;
                Label lblInstrumentName = e.Item.FindControl("lblInstrumentName") as Label;
                CtrtContractItemEntity itemEntity = new CtrtContractItemBll().Get<CtrtContractItemEntity>(new Guid(lblItemID.Text));
                CtrtInstrumentEntity entity = new CtrtInstrumentBll().Get<CtrtInstrumentEntity>(itemEntity.instrumentGUID);
                lblInstrumentName.Text = entity.instrumentName;
            }
        }

        protected void delete(object sender, EventArgs e)
        {
            if (guarantyids.Value.Trim() != "")
            {
                string[] guarantyidsArray = guarantyids.Value.Trim().Split(new char[] { ';' });
                for (int index = 0; index < guarantyidsArray.Length; index++)
                {
                    CtrtItemGuarantyBll bll = new CtrtItemGuarantyBll();
                    CtrtItemGuarantyEntity itemEntity = bll.Get<CtrtItemGuarantyEntity>(new Guid(guarantyidsArray[index]));
                    if (itemEntity != null)
                    {
                        bll.Delete(itemEntity);
                    }
                }
                LoadData();
            }
        }
    }
}
